from my_utils.shell import Shell


class Mysql(Shell):
    def e(self, sql):
        sql = sql.format(ymd=self.ymd).replace("'", '"')
        host = self.get('mysql_host', 'localhost')
        user = self.get('mysql_user', 'root')
        password = self['mysql_pwd']
        db = self['mysql_db']
        return self.sh_cmd_and_alert("mysql -h'{}' -u'{}' -p'{}' -D'{}' -e'{}'".format(
            host,
            user,
            password,
            db,
            sql,
        ))

    def count(self, tb):
        return self.e('SELECT COUNT(1) FROM ' + tb)
